<template>
  <div class="container">
    <div class="boxs">
      <div class="c-name">购物车</div>
      <div class="empty" v-if="list.length == 0">空空如也</div>
      <div class="list-box">
        <div class="c-box" v-for="(item, index) in list" :key="item.id">
          <div class="c-yes" @click="yesEvent(index)">
            <img v-show="item.status === true" src="https://huwei-1312277086.cos.ap-nanjing.myqcloud.com/yes.png" alt="" />
          </div>
          <img class="c-bar" :src="item.image" alt="" />
          <div class="c-right">
            <div class="c-titles">
              <div style="font-size: 20px">{{ item.name }}</div>
              <div>{{ item.sellPoint }}</div>
            </div>
            <div style="margin: 0 50px; color: rgb(24, 134, 238)">{{ item.times }}</div>
            <div class="c-price">¥{{ item.price }}</div>
            <div class="calc">
              <div class="calc-1" @click="acc(item)">-</div>
              <div class="calc-2">{{ item.count }}</div>
              <div class="calc-3" @click="add(item)">+</div>
            </div>
            <div @click="dele(item.id)" style="cursor: pointer; color: crimson">删除</div>
          </div>
        </div>
      </div>
      <div class="c-bot">
        <div class="c-count">
          共: <span>{{ count }}</span
          >件商品
        </div>
        <div class="c-count">
          合计:<span>¥{{ price }}</span>
        </div>
        <div class="c-btn" @click="goRes">结算</div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'FirstProjectShopCard',
  data() {
    return {
      list: [],
      price: 0,
      count: 0,
      checked: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    goRes() {
      if (this.countEvent() == 0) return alert('请选择商品')
      this.$router.push('/res')
    },
    //删除
    async dele(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      const { data: res } = await this.$http.post('del_car', { id })
      if (res.code !== 0) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getList()
    },
    // 按钮状态改变
    yesEvent(i) {
      console.log(this.list[i].status)
      this.$set(this.list[i], 'status', !this.list[i].status)
      this.countEvent()
    },
    acc(item) {
      if (item.count <= 1) return
      item.count--
      this.countEvent()
    },
    add(item) {
      item.count++
      this.countEvent()
    },
    //价格和数量的计算
    countEvent() {
      let arr = this.list.filter(t => t.status)
      let count = 0,
        price = 0
      arr.map(t => {
        count += t.count
        price += t.price * t.count
      })
      this.count = count
      this.price = price
      return arr.length
    },
    //获取购物车数据
    async getList() {
      const { data: res } = await this.$http.get('get_car')
      this.list = res.data
      if (this.list.length == 0) return (this.list = [])
      console.log(res)
    }
  },
  mounted() {}
}
</script>
<style lang="scss" scoped>
.container {
}
.boxs {
  width: 1200px;
  margin: 50px auto;
}

.empty {
  text-align: center;
  padding: 50px;
}

.list-box {
  max-height: 600px;
  overflow-y: auto;
}

.c-name {
  font-size: 30px;
  font-weight: 600;
  text-align: center;
  margin: 20px;
}

.clear {
  padding: 5px 20px;
  border-radius: 5px;
  background: crimson;
  color: #fff;
  width: fit-content;
  float: right;
  cursor: pointer;
}

.c-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgb(138, 135, 135);
  padding: 10px;
  border-radius: 5px;
  margin-bottom: 20px;
  margin-top: 50px;
}

.c-yes {
  width: 25px;
  height: 25px;
  border: 1px solid #666;
  cursor: pointer;
}

.c-yes img {
  width: 100%;
  height: 100%;
}

.c-bar {
  width: 120px;
  min-width: 120px;
  height: 100px;
  margin: 0 20px;
}

.c-right {
  flex-grow: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.c-titles {
  flex-grow: 1;
}

.c-price {
  color: red;
}

.calc {
  display: flex;
  align-content: center;
  margin: 0 100px;
}

.calc-2 {
  margin: 0 10px;
  padding-top: 2px;
}

.calc-1,
.calc-3 {
  width: 25px;
  height: 25px;
  display: flex;
  justify-content: center;
  align-content: center;
  border: 1px solid #666;
  cursor: pointer;
}

.c-bot {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: #ddd;
  border-radius: 5px;
  min-height: 40px;
  margin-top: 40px;
}

.c-count {
  margin: 0 30px;
}

.c-count span {
  color: red;
}

.c-btn {
  width: 150px;
  text-align: center;
  background: rgb(138, 2, 2);
  color: #fff;
  height: 40px;
  line-height: 40px;
  cursor: pointer;
}
::v-deep .el-checkbox__inner {
  width: 20px;
  height: 20px;
  font-size: 30px;
}
</style>
